#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/11/13 12:37
# @Author  : leon yan
# @Site    : 
# @File    : test.py
# @Software: PyCharm
import datetime
import struct
import time
import lib.applets as sApp

aa = "1636772727497"


# bb = []
# for i in range(-1, -(len(aa)+1), -1):
#     bb.append(aa[i])
#
# print(bb)
#
# cc = 162
# digit = cc % 16
# print(digit)
# chaDic = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'}
# cc2 = chaDic.get(digit, str(digit))
# print(cc2)
#
# hexStr = ""
# while cc >= 16:
#     digit = cc % 16
#     hexStr = chaDic.get(digit, str(digit)) + hexStr
#     cc //= 16
# print(hexStr)

def To_hex_str(num):
    chaDic = {10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F'}
    hexStr = ""
    if num < 0:
        num = num + 2 ** 32
    while num >= 16:
        digit = num % 16
        hexStr = chaDic.get(digit, str(digit)) + hexStr
        num //= 16
    hexStr = chaDic.get(num, str(num)) + hexStr
    if len(hexStr) < 2:
        hexStr = "0" + hexStr
    return hexStr


# HexStr = "245154545"  # =20
# aa1 = len(HexStr)
# aa2 = aa1 // 2
# aa3 = aa1 % 2
#
# cc = []
# while aa2 > 0:
#     ss = aa3+(aa2-1)*2
#     cc.append(HexStr[ss:ss+2])
#     aa2 -= 1
# if aa3 >0:
#     cc.append(HexStr[0:aa3])
# print(cc)

# def str_to_hex(s):
#     return ' '.join([hex(ord(c)).replace('0x', '') for c in s])
#
# HexStr = "1EDF"
# print(str_to_hex(HexStr))

def toSaveWord(HexStr):
    aa = len(HexStr)
    sStr = ""
    lstStr = []
    i = aa - 1
    dd = ""
    j = 0
    while i >= 0:
        dd += HexStr[i]
        i -= 1
        j += 1
        if j >= 8 or i == -1:
            j = 0
            lstStr.append(dd)
            dd = ""

    print(lstStr)


# toSaveWord(HexStr)

# aa = 1636772727497
# aa = 99999999
# aa = 256
# cc = To_hex_str(aa)
# print(cc)
# bb = []
# for i in range(-1, -(len(cc)+1), -1):
#     bb.append(cc[i])
# print(bb)
# xx = len(bb)
# yy = xx
# xx2 = xx % 8
# xx //= 8
# print(yy, xx2, xx)
# # xx //= 8
# # print(xx)

# aa = [2,3,4,5,6]
# # print(aa[-1:-(len(aa)+1):-1])
# aa = "000000009"
# aa2 = ""
# for i in range(9):
#     if aa[i] != "0" and aa[i] == ".":
#         aa2 = "0" + aa[i:10]
#         break
#     if aa[i] != "0" and aa[i] != ".":
#         aa2 = aa[i:10]
#         break
# print(aa2)

# aa ="YT@"
# aa2 = aa.split("@")
# print(aa2)

# import re
# a = "123abc456"
# ret_match= re.match('a',"abcde");
# print("1", ret_match.group())  #返回返回被 RE 匹配的字符串
# print("2", ret_match.start())  #返回匹配开始的位置
# print("3", ret_match.end())    #返回匹配结束的位置
# print("4", ret_match.span())   #返回一个元组包含匹配 (开始,结束) 的位置


import re


def xx(theStr, aa):
    pattern = re.compile(theStr)
    msgReObj = pattern.match(aa)
    if msgReObj:
        cc = msgReObj.group()
        return cc, msgReObj
    else:
        return None


# aa = "123456789"
# bb = r'[12345]'
# dd = xx(bb, aa)
# print(dd)

# aa = b'000000.07'
# print(type(aa))
# bb = str(aa, encoding="utf-8")
# print(bb)

def portOut(eNum):
    if eNum <= 4:
        return 1, eNum
    else:
        aa = eNum % 4
        if aa == 0:
            ePort = 4
            eMachinie = eNum // 4
        else:
            ePort = aa
            eMachinie = eNum // 4 + 1
        return eMachinie, ePort


# aa = [2,3,4,5,6]


def rePortOut(eM, eP):
    return (eM - 1) * 4 + eP


# aa = portOut(2)
# aa2 = rePortOut(aa[0], aa[1])
# print(aa)
# print(aa2)
# # aa = [2,3,4,5,6]
# # print(aa[0:3])
#
# aa = ["ppp" + str(11)]
# bb = [1,2,3]
# if len(bb) < 10:
#     for i in range(10-len(bb)):
#         bb.append("/")
# print(aa + bb)
#
# aa = 0
# cc = str(hex(aa).upper())
# print(cc, type(cc))
#
# aa = [1,2,3,4,5,6]
# aa.insert(0,99)
# print(aa)

# def getBytesPack(array_data):
#     bytes_data = bytes()
#     for i in range(len(array_data)):
#         bytes_data += struct.pack('B', array_data[i])
#
#     return bytes_data
#
#
# send_data = [112, 3, 0xcc, 0xbb, 221, 226]
# send_pack = getBytesPack(send_data)
# print(send_pack)

# data = ord('.')
#
# data_byte = data.to_bytes(1, byteorder='little', signed=True)
#
# print(type(data_byte), data_byte, len(data_byte))
#
# data_int = int.from_bytes(data_byte, byteorder='little', signed=True)
#
# print(type(data_int), data_int)
#
# CurrentTime = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
# Timestamp = int(round(time.time() * 10000000))
# print(Timestamp)

# def To_hex_str(num):
#     chaDic = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'}
#     hexStr = ""
#     if num < 0:
#         num = num + 2 ** 32
#     while num >= 16:
#         digit = num % 16
#         hexStr = chaDic.get(digit, str(digit)) + hexStr
#         num //= 16
#     hexStr = chaDic.get(num, str(num)) + hexStr
#     return hexStr

# aa2 = ""
# for i in range(len(aa)):
#     aa2 += To_hex_str(int(aa[i]))
# print(aa2)


def receiveProcess(strData):
    # logger.info("sortClient: receive " + str(strData))
    # print(type(strData))
    cmd = sApp.To_hex_str(int(strData[4]))
    # print(cmd)
    if cmd == '04':
        retStamp = dataCnvStr(strData[8:25])
        retPortNum = dataCnvInt(strData[25:26])
        retPort = dataCnvInt(strData[26:27])
        print(retStamp)
        print(retPortNum)
        print(retPort)


def dataCnvStr(eByte):
    aa = ""
    for i in range(len(eByte)):
        aa += str(chr(int(eByte[i])))
    return aa


def dataCnvInt(eByte):
    aa = ""
    for i in range(len(eByte)):
        aa += str(int(eByte[i]))
    return aa

aa = b'\x00\x1b\x03\x01\x04\x00\x01\x1116379461202298674\x01t\r\n\x00\x15\x02\x01p\x00\x01\x01\x00\x01\x00\x00\x03\x00\x00\x00\x00\x03\x00\x00\x00\r\n'
receiveProcess(aa)

aa = 0.123
print(str(aa))

from datetime import date, timedelta
dt = []
for i in range(5):
    dt.append(date.today() - timedelta(i))
    print('5 days before Current Date :', dt[i])
print('Current Date :', date.today())

aa = [1,2,3,4]
aa.pop(0)
print(aa)

